﻿// =====================================================================
// <copyright file="MainWindow.xaml.cs" author="David R. Guindo">
// 	Copyright (C) 2013. All rights reserved.
// </copyright>
// =====================================================================
// [Fwk2.0]

namespace AppBlock.Config.Demo
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    ///     Lógica de interacción para MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="MainWindow" /> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;
            CbConfigs.SelectionChanged += cbConfigs_SelectionChanged;
        }

        private readonly Dictionary<string, string> oldValues = new Dictionary<string, string>();

        private void cbConfigs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var key in oldValues) ConfigurationManager.AppSettings[key.Key] = "";

            TxLog.Text = "";

            foreach (var key in ConfigManager.LoadConfigurations(1, CbConfigs.SelectedValue.ToString()))
            {
                if (!oldValues.ContainsKey(key.Key))
                    oldValues.Add(key.Key, key.Value);
                else
                    oldValues[key.Key] = "";
            }


            if (CbConfigs.SelectedValue.ToString() == "")
                PrintInfo(TxLog, "without environment specified. Default values ​​were shown.");
            else
                PrintInfo(TxLog, string.Format("Entorno '{0}':", CbConfigs.SelectedValue));

            PrintInfo(TxLog, Environment.NewLine);

            foreach (string key in ConfigurationManager.AppSettings.AllKeys)
            {
                if (ConfigurationManager.AppSettings[key] != "")
                {
                    PrintInfo(TxLog, string.Format("{0,-39}\t= {1}", key, ConfigurationManager.AppSettings[key]));
                }
            }
        }

        private void PrintInfo(TextBox txt, string message)
        {
            txt.Text += message + Environment.NewLine;
        }

        /// <summary>
        ///     Handles the Loaded event of the MainWindow control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            CbConfigs.Items.Add("");

            foreach (string d in Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory + "\\Config"))
            {
                CbConfigs.Items.Add(d.Substring(d.LastIndexOf("\\") + 1));
            }

           // CbConfigs.SelectedIndex = 0;
        }


        private const string Section = "appSettings";

        private void btEncrypt_Click(object sender, RoutedEventArgs e)
        {
            if (CbConfigs.SelectedValue == null)
            {
                MessageBox.Show("Select config profile!");
                return;
            }

            ConfigManager.EncryptSectionFiles(Section, 1, CbConfigs.SelectedValue.ToString());
            MessageBox.Show("ApSettings encrypted!");
        }

        private void btDecrypt_Click(object sender, RoutedEventArgs e)
        {
            if (CbConfigs.SelectedValue == null)
            {
                MessageBox.Show("Select config profile!");
                return;
            } 
            
            ConfigManager.DecryptSectionFiles(Section, 1, CbConfigs.SelectedValue.ToString());
            MessageBox.Show("ApSettings decrypted!");
        }
    }
}